/**
 * 
 */
package fr.ants.view.component;

import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import fr.ants.controller.clock.ClockState;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.view.HMI;
import fr.ants.view.Items;

/**
 * It's the tool on the top of the frame
 * 
 * @author Nyu
 */
public class ToolBar extends JMenuBar implements PropertyChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2240824282293442583L;
	/**
	 * file menu.
	 */
	private final JMenu fileMenu;
	/**
	 * simulation menu.
	 */
	private final JMenu simulationMenu;
	/**
	 * parameters menu.
	 */
	// private final JMenu parametersMenu;

	/**
	 * accelerate Button.
	 */
	private JMenuItem accelerateButton;
	/**
	 * slow button.
	 */
	private JMenuItem slowButton;
	/**
	 * start button.
	 */
	private JMenuItem startButton;
	/**
	 * stop button.
	 */
	private JMenuItem stopButton;

	/**
	 * add obstacle button.
	 */
	// private JMenuItem addObstacleButton;
	/**
	 * add resource button;
	 */
	// private JMenuItem addResourceButton;
	/**
	 * Canvas.
	 */
	// private Canvas canvas;

	/**
	 * Build the tool bar.
	 * 
	 * @param buttonsListener
	 *            listener for the buttons.
	 */
	public ToolBar(final ButtonsListener buttonsListener) {
		// file menu
		fileMenu = new JMenu(Items.FILE.getLabel());
		createFileMenu(fileMenu, buttonsListener);
		add(fileMenu);

		// simulation menu
		simulationMenu = new JMenu(HMI.SIMULATION);
		createSimulationMenu(simulationMenu, buttonsListener);
		add(simulationMenu);

		// parameters menus
		// parametersMenu = new JMenu(HMI.PARAMETER);
		// createParametreMenu(parametersMenu, buttonsListener);
		// add(parametersMenu);
	}

	/**
	 * create the file menu.
	 * 
	 * @param pFileMenu
	 * @param buttons
	 *            listener
	 */
	private void createFileMenu(final JMenu pFileMenu,
			final ButtonsListener buttonsListener) {
		// open map
		final JMenuItem openMapButton = new JMenuItem(Items.LOAD.getLabel(),
				KeyEvent.VK_O);
		openMapButton.setActionCommand(Items.LOAD.getActionCommand());
		openMapButton.addActionListener(buttonsListener);
		pFileMenu.add(openMapButton);

		// recent maps
		/*
		 * JMenuItem recentMapsButton = new
		 * JMenuItem(Items.RECENT_MAP.getLabel());
		 * recentMapsButton.setActionCommand
		 * (Items.RECENT_MAP.getActionCommand());
		 * recentMapsButton.addActionListener(buttonsListener);
		 * pFileMenu.add(recentMapsButton);
		 */

		pFileMenu.addSeparator();

		// exit
		final JMenuItem exitButton = new JMenuItem(Items.EXIT.getLabel());
		exitButton.setActionCommand(Items.EXIT.getActionCommand());
		exitButton.addActionListener(buttonsListener);
		pFileMenu.add(exitButton);

	}

	/**
	 * create the simulation menu.
	 * 
	 * @param pSimulationMenu
	 *            menu simulation
	 * @param buttonsListener
	 *            buttons listener
	 */
	private void createSimulationMenu(final JMenu pSimulationMenu,
			final ButtonsListener buttonsListener) {

		// accelerate
		accelerateButton = new JMenuItem(Items.ACCELERATE.getLabel());
		accelerateButton.setActionCommand(Items.ACCELERATE.getActionCommand());
		accelerateButton.addActionListener(buttonsListener);
		accelerateButton.setEnabled(false);
		pSimulationMenu.add(accelerateButton);

		// slow
		slowButton = new JMenuItem(Items.SLOW.getLabel());
		slowButton.setActionCommand(Items.SLOW.getActionCommand());
		slowButton.addActionListener(buttonsListener);
		slowButton.setEnabled(false);
		pSimulationMenu.add(slowButton);

		pSimulationMenu.addSeparator();

		// start
		startButton = new JMenuItem(Items.PLAY.getLabel(), KeyEvent.VK_P);
		startButton.setActionCommand(Items.PLAY.getActionCommand());
		startButton.setToolTipText(Items.PLAY.getLabel());
		startButton.addActionListener(buttonsListener);
		startButton.setEnabled(false);
		pSimulationMenu.add(startButton);

		// stop
		stopButton = new JMenuItem(Items.STOP.getLabel(), KeyEvent.VK_P);
		stopButton.setActionCommand(Items.STOP.getActionCommand());
		stopButton.addActionListener(buttonsListener);
		stopButton.setEnabled(false);
		pSimulationMenu.add(stopButton);

	}

	/**
	 * create the parameter menu.
	 * 
	 * @param pParametreMenu
	 *            menu parameter
	 * @param buttonsListener
	 *            buttons listener
	 */
	// private void createParametreMenu(final JMenu pParametreMenu,
	// final ButtonsListener buttonsListener) {
	// // add obstacle
	// addObstacleButton = new JMenuItem(
	// Items.OBSTACLE_HERE.getLabel());
	// addObstacleButton.setActionCommand(Items.OBSTACLE_HERE
	// .getActionCommand());
	// addObstacleButton.addActionListener(buttonsListener);
	// pParametreMenu.add(addObstacleButton);
	//
	// // add resource
	// addResourceButton = new JMenuItem(
	// Items.RESOURCE_HERE.getLabel());
	// addResourceButton.setActionCommand(Items.RESOURCE_HERE
	// .getActionCommand());
	// addObstacleButton.addActionListener(buttonsListener);
	// pParametreMenu.add(addResourceButton);
	//
	// }

	@Override
	public void propertyChange(final PropertyChangeEvent evt) {

		// if (canvas == null || canvas.getGrid() == null) {
		// addObstacleButton.setEnabled(false);
		// addResourceButton.setEnabled(false);
		// } else {
		// addObstacleButton.setEnabled(true);
		// addResourceButton.setEnabled(true);
		// }

		if ("grid".equals(evt.getPropertyName())) {
			startButton.setEnabled(true);
		}
		if ("state".equals(evt.getPropertyName())) {
			if (evt.getNewValue() instanceof ClockState) {
				ClockState state = (ClockState) evt.getNewValue();
				switch (state) {
				case PAUSED:
					startButton.setText(Items.PLAY.getLabel());
					startButton.setToolTipText(Items.PLAY.getLabel());
					accelerateButton.setEnabled(false);
					slowButton.setEnabled(false);
					stopButton.setEnabled(true);
					break;
				case RUNNING:
					startButton.setText(Items.PAUSE.getLabel());
					startButton.setToolTipText(Items.PAUSE.getLabel());
					accelerateButton.setEnabled(true);
					slowButton.setEnabled(true);
					stopButton.setEnabled(true);
					break;
				case STOPPED:
					startButton.setText(Items.PLAY.getLabel());
					startButton.setToolTipText(Items.PLAY.getLabel());
					accelerateButton.setEnabled(false);
					slowButton.setEnabled(false);
					stopButton.setEnabled(false);
					break;
				default:
					break;
				}
			}
		}

	}

	/*
	 * public void setCanvas(Canvas canvas) { this.canvas = canvas;
	 * 
	 * }
	 */
}